package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.PartsManagement.BusinessTypeTable;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.PartsManagement.TeamPersonnel;
import com.cn.bean.vo.bc.UserJob;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.BusinessTypeDao;
import com.cn.mapper.dao.db5.PartsManagement.TeamPersonnelDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.PartsManagement.TeamPersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Transactional
public class TeamPersonnelServiceImpl implements TeamPersonnelService {
    private final TeamPersonnelDao teamPersonnelDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public TeamPersonnelServiceImpl(TeamPersonnelDao teamPersonnelDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {
        this.teamPersonnelDao = teamPersonnelDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
    }

    @Override
    public List<TeamPersonnel> selectTeamPersonnel(int limit, int page, String username, String name, String jobName, String teamName) {
        return teamPersonnelDao.selectTeamPersonnel(limit, page, username, name, jobName, teamName);
    }

    @Override
    public List<TeamPersonnel> selectTeamAuto1(int limit, int page, String code, String name) {
        return teamPersonnelDao.selectTeamAuto1(limit,page,code,name);
    }

    @Override
    public List<TeamPersonnel> selectTeam(int limit, int page, String code, String name) {
        return teamPersonnelDao.selectTeam(limit, page, code, name);
    }

    @Override
    public List<UserJob> selectEmployeeJob(int limit, int page, String jobName, String name, String username) {
        return teamPersonnelDao.selectEmployeeJob(limit, page, jobName, name, username);
    }

    @Override
    public Result addTeam(String TeamName, String description, int status, String CreationTime, List<TeamPersonnel> childList) {
        Integer nameCount = teamPersonnelDao.selectTeamCount(TeamName);
        if (nameCount > 0) {
            return Result.failure("500", "班组名称重复");
        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("TeamManagement");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "TeamManagement");
        int i = teamPersonnelDao.addTeam(ID, code, TeamName, description, status, CreationTime);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        for (TeamPersonnel teamPersonnel : childList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("TeamPersonnelMapping");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "TeamPersonnelMapping");

            int i1 = teamPersonnelDao.addEmployeeJob(ID1, ID, teamPersonnel.getUserId(), teamPersonnel.getJobId(), teamPersonnel.getTPMStatus());
            if (i1 == 0) {

                return Result.failure("500", "添加失败");
            }
        }
        return Result.success("200", "添加成功");
    }


    @Override
    public Result deleteTeamEmployeeJob(List<TeamPersonnel> idList) {
        for (TeamPersonnel teamPersonnel : idList) {
            int i = teamPersonnelDao.deleteTeam(teamPersonnel.getTeamID());
            int i1 = teamPersonnelDao.deleteEmployeeJob(teamPersonnel.getTeamID());
            if (i == 0 ) {
                return Result.failure("500", "删除失败");
            }
        }
        return Result.success("200", "删除成功");

    }

    @Override
    public List<TeamPersonnel> selectTeamPersonnelTeamId(int TeamID) {
        return teamPersonnelDao.selectTeamPersonnelTeamId(TeamID);
    }

    @Override
    public Result upDateTeam(int TeamID, String TeamName, String description, int status, List<TeamPersonnel> childList) {
        List<TeamPersonnel> teamPersonnels = teamPersonnelDao.selectAllTeam();
        for (TeamPersonnel teamPersonnel : teamPersonnels) {
            if (teamPersonnel.getTeamName().equals(TeamName) && teamPersonnel.getTeamID() != TeamID) {
                return Result.failure("500", "班组名称重复");
            }
        }
        int i = teamPersonnelDao.upDateTeam(TeamID, TeamName, description, status);
        if (i == 0) {
            return Result.failure("500", "修改失败");
        }
        int i1 = teamPersonnelDao.deleteEmployeeJob(TeamID);
        if (i1 == 0) {
            return Result.failure("500", "修改失败");
        }
        for (TeamPersonnel teamPersonnel : childList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("TeamPersonnelMapping");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "TeamManagement");

            int i2 = teamPersonnelDao.addEmployeeJob(ID1, TeamID, teamPersonnel.getUserId(), teamPersonnel.getJobId(), teamPersonnel.getTPMStatus());
            if (i2 == 0) {
                return Result.failure("500", "修改失败");
            }
        }
        return Result.success("200", "修改成功");
    }

    @Override
    public List<UserJob> selectUserInformation(int limit, int page, String name, String username) {

        return teamPersonnelDao.selectUserInformation(limit, page, name, username);
    }

    @Override
    public List<UserJob> selectJobInformation(int limit, int page, String jobName) {
        return teamPersonnelDao.selectJobInformation(limit, page, jobName);
    }
}
